﻿using System;
using BoldBrick.Core.Logging;
using BoldBrick.OneIntranet.HR.DataObjects;
using BoldBrick.OneIntranet.HR.Resources;
using BoldBrick.OneIntranet.HR.Utils;
using BoldBrick.SharePoint.CommonControls.Layouts;
using Microsoft.SharePoint;
using BoldBrick.SharePoint.CommonControls.Extensions;

namespace BoldBrick.OneIntranet.HR.Layouts
{
	public partial class OpenPositionPage : BaseDialogPage
	{
		private static readonly ILogger log = Logger.GetLogger(typeof(OpenPositionPage));

		#region ------ Load Page ------------------------------------------------------------------

		protected override void OnLoad(EventArgs e)
		{
			this.buttonMakeACopy.Text = ResourceBuilder.GetResource("OpenPositionPage_buttonMakeACopy_Text", SPContext.Current.Web.Language);

			base.OnLoad(e);
		}

		#endregion

		#region ------ BaseDialogPage Members -----------------------------------------------------

		protected override string GetDescription(uint language)
		{
			this.DialogIconType = DialogIconTypes.Question;

			SPListItem item = GetListItem(language);
			if (item == null)
				return null;

			DateTime? closedOnDate = item.GetValue<DateTime>(SiteColumns.ClosedOnColumn.InternalName);
			string description = null;
			if (closedOnDate.HasValue)
			{
				description = ResourceBuilder.GetResource("OpenPositionPage_Description", language, closedOnDate.Value.ToShortDateString());
			}
			return description;
		}

		protected override string GetCancelButtonText(uint language)
		{
			return ResourceBuilder.GetResource("OpenPositionPage_CancelButton_Text", language);
		}

		protected override string GetOKButtonText(uint language)
		{
			return ResourceBuilder.GetResource("OpenPositionPage_OkButton_Text", language);
		}

		protected override string GetTitle(uint language)
		{
			return ResourceBuilder.GetResource("OpenPositionPage_Title", language);
		}

		#endregion

		#region ------ Action Methods -------------------------------------------------------------

		protected override bool DoAction(out string errorMessage)
		{
			errorMessage = null;

			uint language = SPContext.Current.Web.Language;
			SPListItem item = GetListItem(language);
			if (item == null)
				return false;

			try
			{
				PositionsHelper.OpenPosition(item);
			}
			catch (Exception ex)
			{
				log.Write(LogLevel.Error, ex, "'PositionsHelper.OpenPosition' method failed.");
				errorMessage = ex.Message;
				return false;
			}

			return true;
		}

		/// <summary>
		/// Creates a copy of selected position
		/// </summary>
		private void CopyPosition()
		{
			uint language = SPContext.Current.Web.Language;

			SPListItem item = GetListItem(language);
			if (item == null)
				return;
			
			// perform copy operation
			int? itemIDNew;
			try
			{
				itemIDNew = PositionsHelper.CopyPosition(item);
			}
			catch (Exception ex)
			{
				log.Write(LogLevel.Error, ex, "'PositionsHelper.CopyPosition' method failed.");
				ShowError(ex.Message, true);
				return;
			}

			if (!itemIDNew.HasValue)
			{
				string errorMessage = ResourceBuilder.GetResource("OpenPositionPage_Error_NoNewItemID", language);
				ShowError(errorMessage, true);
				return;
			}

			string url = item.ParentList.GetListItemUrl(PAGETYPE.PAGE_EDITFORM, itemIDNew.Value);
			Redirect(url, true);
		}

		#endregion

		#region ------ Events ---------------------------------------------------------------------

		protected void buttonMakeACopy_Click(object sender, EventArgs e)
		{
			CopyPosition();
		}

		#endregion
	}
}
